package com.cjx.yuanshenstartforumbackend.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.system.UserInfo;
import com.cjx.yuanshenstartforumbackend.common.BaseResponse;
import com.cjx.yuanshenstartforumbackend.common.ErrorCode;
import com.cjx.yuanshenstartforumbackend.common.ResultUtils;
import com.cjx.yuanshenstartforumbackend.domain.dto.LoginDto;
import com.cjx.yuanshenstartforumbackend.domain.dto.RegisterDto;
import com.cjx.yuanshenstartforumbackend.domain.vo.UserVo;
import com.cjx.yuanshenstartforumbackend.exception.MyException;
import com.cjx.yuanshenstartforumbackend.service.UserService;
import com.cjx.yuanshenstartforumbackend.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Struct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 用户相关功能
 * @Author cjx
 * @Date 2023/12/21 14:49
 **/
@RestController
@RequestMapping("/user")
//@CrossOrigin()
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     * @param loginDto 登录参数
     * @return token
     */
    @PostMapping("/login")
    public BaseResponse<Map<String,String>> loginUser(@RequestBody LoginDto loginDto, HttpSession session){
        //判空处理
        if (loginDto == null){
            throw new MyException(ErrorCode.NULL_ERROR);
        }
        String token = userService.login(loginDto, session);
        Map<String,String> result = new HashMap<>(10);
        result.put("token", token);
        return ResultUtils.success(result);
    }

    @GetMapping("/checkUserName")
    public BaseResponse<?> checkUsername(@RequestParam String username){
        //判空处理
        if (StrUtil.isBlank(username)){
            throw new MyException(ErrorCode.NULL_ERROR);
        }
        return userService.checkUsername(username);
    }

    @PostMapping("/register")
    public BaseResponse<?> registerUser(@RequestBody RegisterDto registerDto){
        //判空处理
        if (registerDto == null){
            throw new MyException(ErrorCode.NULL_ERROR);
        }
        return userService.registerUser(registerDto);
    }

    @GetMapping("/loginOut")
    public BaseResponse<?> loginOut(HttpServletRequest request){
        return userService.loginOut(request);
    }

    /**
     * 获取当前用户信息
     * @return 脱敏的用户信息
     */
//    @CrossOrigin(origins = "http://localhost:5173", allowedHeaders = {"content-type", "Token"})
    @GetMapping("/getUserInfo")
    public BaseResponse<UserVo> getUserInfo(HttpServletResponse response){
        //从threadLocal中获取用户信息
        UserVo user = UserHolder.getUser();
        response.setHeader("Access-Control-Allow-Headers", "*");
        return ResultUtils.success(user);
    }

    @GetMapping("logout")
    public BaseResponse<?> logoutUser(@RequestParam Long uid){
        return userService.logoutUser(uid);
    }

}
